<?php 

/* +-------------------------------------------------------------+
* | Copyright (c) 2009.04.03.QQ:84523559 All rights reserved.        
* +-------------------------------------------------------------+
* | img_sy: 图像处理类
* | water(保存地址,水印图片,水印位置,透明度)
* | Str2Pic($string,$source,$destination,$fontUrl,8,"#FFFFFF","#808080",5);将一行字串写入图片中
* +-------------------------------------------------------------+
*/
class image_sy {
        // 当前图片
        protected $img;
        protected $x;
        protected $y;
        // 图像types 对应表
        protected $types = array(
                        1 => 'gif',
                        2 => 'jpg',
                        3 => 'png',
                        6 => 'bmp'
                    );
        
        // image
        public function __construct($img=''){
                !$img && $this->param($img);
        }
        
        // Info
        public function param($img){
                $this->img = $img;
                return $this;
        }
        
        // imageInfo
        public function getImageInfo($img){
                $info = @getimagesize($img);
               // var_dump($img);
                if(isset($this->types[$info[2]])){
                        $info['ext'] = $info['type'] = $this->types[$info[2]];
                } else{
                        $info['ext'] = $info['type'] = 'jpg';
                }
                $info['type'] == 'jpg' && $info['type'] = 'jpeg';
                $info['size'] = @filesize($img);
                return $info;
        }
        
        // thumb(新图地址, 宽, 高, 裁剪, 允许放大)
        public function thumb($filename,$new_w=160,$new_h=120,$cut=0,$big=0){
        // 获取原图信息   
        $info  = $this->getImageInfo($this->img);
        if(!empty($info[0])) {
            $old_w  = $info[0];
            $old_h  = $info[1];
            $type   = $info['type'];
            $ext    = $info['ext'];
            unset($info);
            // 如果原图比缩略图小 并且不允许放大
            if($old_w < $new_h && $old_h < $new_w && !$big){
                    return false;
            }
            // 裁剪图片
            if($cut == 0){ // 等比列
                    $scale = min($new_w/$old_w, $new_h/$old_h); // 计算缩放比例
                    $width  = (int)($old_w*$scale); // 缩略图尺寸
                    $height = (int)($old_h*$scale);
                    $start_w = $start_h = 0;
                    $end_w = $old_w;
                    $end_h = $old_h;
            } elseif($cut == 1){ // center center 裁剪
                        $scale1 = round($new_w/$new_h,2);
                        $scale2 = round($old_w/$old_h,2);
                        if($scale1 > $scale2){
                                $end_h = round($old_w/$scale1,2);
                                $start_h = ($old_h-$end_h)/2;
                                $start_w  = 0;
                                $end_w    = $old_w;
                        } else{
                                $end_w  = round($old_h*$scale1,2);
                                $start_w  = ($old_w-$end_w)/2;
                                $start_h = 0;
                                $end_h   = $old_h;
                        }
                        $width = $new_w;
                    $height= $new_h; 
                } elseif($cut == 2){ // left top 裁剪           
                        $scale1 = round($new_w/$new_h,2);
                    $scale2 = round($old_w/$old_h,2);
                    if($scale1 > $scale2){
                            $end_h = round($old_w/$scale1,2);
                            $end_w = $old_w;
                    } else{
                            $end_w = round($old_h*$scale1,2);
                            $end_h = $old_h;
                    }
                    $start_w = 0;
                    $start_h = 0;
                    $width = $new_w;
                    $height= $new_h; 
                }
            // 载入原图
            $createFun  = 'ImageCreateFrom'.$type;
            $oldimg     = $createFun($this->img); 
            // 创建缩略图
            if($type!='gif' && function_exists('imagecreatetruecolor')){
                $newimg = imagecreatetruecolor($width, $height); 
            } else{
                $newimg = imagecreate($width, $height); 
            }
            // 复制图片
            if(function_exists("ImageCopyResampled")){
                    ImageCopyResampled($newimg, $oldimg, 0, 0, $start_w, $start_h, $width, $height, $end_w,$end_h); 
            } else{
                ImageCopyResized($newimg, $oldimg, 0, 0, $start_w, $start_h, $width, $height, $end_w,$end_h);
            } 

            // 对jpeg图形设置隔行扫描
            $type == 'jpeg' && imageinterlace($newimg,1);
            // 生成图片
            $imageFun = 'image'.$type;
            //var_dump($filename);
            !@$imageFun($newimg,$filename) && die('保存失败!检查目录是否存在并且可写?');
            ImageDestroy($newimg);
            ImageDestroy($oldimg);
            return $filename;
        }
        return false;
    }
    
    // water(保存地址,水印图片,水印位置,透明度)
        public function water($filename,$water,$pos=0,$pct=80){
                // 加载水印图片
                $info = $this->getImageInfo($water);
                //var_dump(!empty($info[0]));
                if(!empty($info[0])){
                        $water_w = $info[0];
                        $water_h = $info[1];
                        $type = $info['type'];
                        $fun  = 'imagecreatefrom'.$type;
                        $waterimg = $fun($water);
                } else{
                        return false;
                }
                // 加载背景图片
                $info = $this->getImageInfo($this->img);
              //  var_dump(!empty($info[0]));
                if(!empty($info[0])){
                        $old_w = $info[0];
                        $old_h = $info[1];
                        $type  = $info['type'];
                        $fun   = 'imagecreatefrom'.$type;
                        $oldimg = $fun($this->img);
                } else{
                        return false;
                }
                // 剪切水印
                $water_w >$old_w && $water_w = $old_w;
                $water_h >$old_h && $water_h = $old_h;
                
                // 水印位置                        
                switch($pos){
                        case 0://随机 
                    $posX = rand(0,($old_w - $water_w)); 
                    $posY = rand(0,($old_h - $water_h)); 
                    break; 
                case 1://1为顶端居左 
                    $posX = 0; 
                    $posY = 0; 
                    break; 
                case 2://2为顶端居中 
                    $posX = ($old_w - $water_w) / 2; 
                    $posY = 0; 
                    break; 
                case 3://3为顶端居右 
                    $posX = $old_w - $water_w; 
                    $posY = 0; 
                    break; 
                case 4://4为中部居左 
                    $posX = 0; 
                    $posY = ($old_h - $water_h) / 2; 
                    break; 
                case 5://5为中部居中 
                    $posX = ($old_w - $water_w) / 2; 
                    $posY = (($old_h - $water_h) / 1.3); 
                    break; 
                case 6://6为中部居右 
                    $posX = $old_w - $water_w; 
                    $posY = ($old_h - $water_h) / 2; 
                    break; 
                case 7://7为底端居左 
                    $posX = 0; 
                    $posY = $old_h - $water_h; 
                    break; 
                case 8://8为底端居中 
                    $posX = ($old_w - $water_w) / 2; 
                    $posY = $old_h - $water_h; 
                    break; 
                case 9://9为底端居右 
                    $posX = $old_w - $water_w; 
                    $posY = $old_h - $water_h; 
                    break; 
                default: //随机 
                    $posX = rand(0,($old_w - $water_w)); 
                    $posY = rand(0,($old_h - $water_h)); 
                    break;   
                }
            // 设定图像的混色模式 
                imagealphablending($oldimg, true); 
                // 添加水印
                imagecopymerge($oldimg, $waterimg, $posX, $posY, 0, 0, $water_w,$water_h,$pct);
                $fun = 'image'.$type;
                !@$fun($oldimg, $filename) && die('保存失败!检查目录是否存在并且可写?'); 
                  imagedestroy($oldimg);
                  imagedestroy($waterimg);
                  return $filename;        
        }
        
    /*    
	将一行字串写入图片中
	支持三种图片扩展名：jpeg、jpg、gif、png
	参数说明
	$string ：写入的文字。如果字库不支持中文，请勿使用中文
	$source ：读取的原始图片地址。不支持远程图片
	$destination ：生成的目标图片地址。为空不生成。可以与原始图片地址相同
	$fontUrl ：字库文件地址。
	$fontSize ：文字字体大小
	$shadowColor ：文字阴影颜色。16进制
	$fontColor ：文字字体颜色。16进制
	$pos  位置
	#函数调用
	Str2Pic(写入的文字,原始图片地址,目标图片地址,字库文件地址,文字字体大小,文字阴影颜色,文字x坐标位置,文字y坐标位置)
	Str2Pic($string,$source,$destination,$fontUrl,8,"#FFFFFF","#808080",10,10);
	Str2Pic("[url=http://www.zh5j.com]www.zh5j.com","music.gif","music.jpg","image/swissk.ttf",10,"FFFFFF","330099",10,10[/url]);
	***************************************************************************************************/
	function Str2Pic ($string,$source,$destination="",$fontUrl,$fontSize=10,$shadowColor="#FFFFFF",$pos=0) {
			global $rootpath;
			$color=$this->hex2dec($shadowColor);
			$info = $this->getImageInfo($source);
			if(!empty($info)){
				$old_w=$info[0];
				$old_h=$info[1];
			}
			$new_w=$old_w*0.02;
			$new_h=$old_h*0.02;
			$im = imagecreatetruecolor(400, 30);
			$white = imagecolorallocate($im, 128, 128, 128);
			$grey = imagecolorallocate($im, 0, 0, 0);
			$black = imagecolorallocate($im, $color[0], $color[1], $color[2]);
			imagefilledrectangle($im, 0, 0, 399, 29, $white);
			$white=imagecolortransparent($im,$white);
			imagettftext($im, $fontSize, 0, 11, 21, $grey, $fontUrl, $string);
			imagettftext($im, $fontSize, 0, 10, 20, $black, $fontUrl, $string);
			imagepng($im,$rootpath.'upload/123.png');
			imagedestroy($im);
			//$this->param($rootpath.'upload/123.png')->thumb('12388.jpg', $new_w,$new_h,0);
			$this->param($source);
			if($this->water($destination,$rootpath.'upload/123.png',$pos,80)){
				return true;
			}
	}
	
	function hex2dec($HexColror) {#十六进制颜色转换成10进制颜色
	    preg_match_all("/([0-f]){2,2}/i",$HexColror,$matches);
	    if(count($matches[0])==3){
	    $rr=hexdec($matches[0][0]);
	    $gg=hexdec($matches[0][1]);
	    $bb=hexdec($matches[0][2]);
	    }
	    return array($rr,$gg,$bb);
	}
	        
}
/*
$img1 = 'test.jpg';
// 水印后的图片
$img2 = 'test_new.jpg';
// 水印
$water = 'news.png';
$img = new image();
// 等比缩放
$img->param($img1)->thumb('test_0.jpg', 200,200,0);
// center center 裁剪
$img->param($img1)->thumb('test_1.jpg', 200,200,1);
// top left 裁剪
$img->param($img1)->thumb('test_2.jpg', 200,200,2);
// 右下角添加水印
$img->param($img1)->water($img2,$water,5,30);*/

?>
